/************************************  

DESCRIPTION: This is the ols regression run separately for men/women on the full sample.
 5-digit zip codes interacted with race controls. This is just hypertension and diabetes. death done separately
 
 Datasets used: 
  (1) maleLPManalysis2
  (2) femLPManalysis2

 Datasets created: 
  (1-2) fembetaDiab malebetaDiab
  (3-4) malebetaHyp fembetaHyp
  
 ************************************/  

set more off
capture log close
clear all
cd "\\192.168.6.107\silstorage\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Program\PropensityScore\Patients\ResponsetoReferee\OtherPropScoreMethods\LPM"

set matsize 10000

*global base "N:"
global base "\\192.168.6.107\silstorage"
 
global origData "$base\MedicareClaims-P045601-BE"
global dataIn "$base\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-In\"
global dataOut "$base\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out"
global dpath "$base\MedicareClaims-P045601-BE\Work\ay_data"
global dataProp "$base\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore\Patients"
global logs "$base\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Logs\PropScore\Patients"
global dpath "$base\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "$base\MedicareClaims-P045601-BE\Work\ska"
global output

adopath +  "$base/SIL-Common/estout"
adopath +  "$base/SIL-Common/outreg2"
adopath +  "$base/SIL-Common/reghdfe-master/package"

log using "$logs/2LPMregs.log", replace


*Columnames program

	program getColnames 
	 quietly {
	  matrix A = e(b)'
	 
	 local rownames : rowfullnames A
	 local c : word count `rownames'
	 local names : colfullnames A

	 svmat A, names(col)

	 gen rownames = ""
	 forvalues i = 1/`c' {

	  replace rownames = "`:word `i' of `rownames''" in `i' 
	   } 

	 }

	end


	program getRegStats
	 quietly {
	  matrix A = e(b)'
	 
	 local rownames : rowfullnames A
	 local c : word count `rownames'
	 local n = `c' + 1
	 local rsq = `c' + 3

	 replace beta1 =e(N) in `n' 
	 replace beta1 =e(r2_p) in `rsq' 

	 }

	end




 *Run Linear probability analysis
		 foreach looper in "fem" "male" {
			  use `looper'LPManalysis2.dta, replace
			  *These are big deals. Drops  18mm obs. Explored the reason for this in prior programs. mainly due to age. some due to 5% sample issues.
			 keep if mxdiabetes_p_all==1 
			 capture drop urban_rural
				 gen urban_rural=urbanrural1+2*urbanrural2
				  replace urban_rural=3 if urban_rural==0
				  capture rename mxicd8_chronic mxicd8_cardiac
			 capture drop spec35 spec97 spec66 spec04 spec16 spec05 spec43 spec08 spec01 spec11 spec93 spec83 spec39 spec66 spec04 spec16 
				 
			 capture drop zip3
				 gen zip3 = substr(zip_pat,1,3)
				 encode zip3, gen(zip3n)
				 
				 
			*Diabetes 
				 foreach var of varlist oc_diabcomp1 oc_glaucoma oc_diabcomp2 oc_death {
					 if regexm("`var'","oc_([a-z0-9]+)$") { 
					  local pre = "`=regexs(1)'"
						}
		 
					 di "`pre'"
					 
					 capture rename mxicd3_chronic mxicd3_endocrine 
					 
					areg `var' i.postmerger  i.vmerger  i.vmerger#c.nyear i.age##i.rac_white i.qtr##i.urban_rural mxicd1_acute mxicd5_acute mxicd18_acute mxicd*_c* fmp nptf anesth spec* fs* lnvisit* if mxdiabetes_p_all==1 & kv_`pre'==1, abs(zipwhite) cluster(zip3n )   
					  matrix beta  = e(b)' 
					  matrix sesq = vecdiag(e(V))' 
					  svmat beta
					  getColnames
					  getRegStats
					  svmat sesq
					   rename rownames rn`pre'_`i'
					  capture drop y1 
					  capture drop j 
					  capture drop i
					   rename beta1 beta_`pre'_`i'
					   rename sesq1 sesq_`pre'_`i'
					 
					 
					  matrix B = e(b)
					  local nvars = colsof(B)
					 
					   replace rn`pre'_`i' = "N" if _n == `nvars' + 1
					   replace beta_`pre'_`i' = e(N) if _n == `nvars' + 1
					 
					   replace rn`pre'_`i' = "R2" if _n == `nvars' + 2
					   replace beta_`pre'_`i' = e(r2) if _n == `nvars' + 2
					 
					   replace rn`pre'_`i' = "aR2" if _n == `nvars' + 3
					   replace beta_`pre'_`i' = e(r2_a) if _n == `nvars' + 3
					 
					 }
 
	 keep rn* beta* sesq*
	 drop if _n>10000
	 save ./`looper'betaDiab.dta, replace
 clear
 
			use `looper'LPManalysis2.dta
			 keep if mxhypertension_p_all==1 
			  capture drop zip3
			 gen zip3 = substr(zip_pat,1,3)
			 encode zip3, gen(zip3n)
			   capture drop urban_rural
			   gen urban_rural=urbanrural1+2*urbanrural2
			 replace urban_rural=3 if urban_rural==0

			 
			 
			*Hypertension 
				 foreach var of varlist oc_acuteicd8 oc_ami oc_ihd oc_death {
					 if regexm("`var'","oc_([a-z0-9]+)$") { 
					  local pre = "`=regexs(1)'"
					 }
					 
				 di "`pre'"
				 
				 capture rename mxicd3_endocrine mxicd3_chronic 
				 capture rename mxicd8_chronic mxicd8_cardiac
				 
				areg `var' i.postmerger  i.vmerger  i.vmerger#c.nyear i.age##i.rac_white i.qtr##i.urban_rural mxicd1_acute mxicd5_acute mxicd18_acute mxicd*_ch* fmp nptf anesth spec* fs* lnvisit* if mxhypertension_p_all==1 & kv_`pre'==1, abs(zipwhite) cluster(zip3n) 

				  matrix beta  = e(b)' 
				  matrix sesq = vecdiag(e(V))' 
				  svmat beta
				  getColnames
				  getRegStats
				  svmat sesq
				  rename rownames rn`pre'_`i'
				  capture drop y1 
				  capture drop j 
				  capture drop i
				  rename beta1 beta_`pre'_`i'
				  rename sesq1 sesq_`pre'_`i'
				  matrix B = e(b)
				  local nvars = colsof(B)
				 
				  replace rn`pre'_`i' = "N" if _n == `nvars' + 1
				  replace beta_`pre'_`i' = e(N) if _n == `nvars' + 1
				 
				  replace rn`pre'_`i' = "R2" if _n == `nvars' + 2
				  replace beta_`pre'_`i' = e(r2) if _n == `nvars' + 2
				 
				  replace rn`pre'_`i' = "aR2" if _n == `nvars' + 3
				  replace beta_`pre'_`i' = e(r2_a) if _n == `nvars' + 3
				  
				  }

			 keep rn* beta* sesq*
			 drop if _n>1000
			 save ./`looper'betaHyp.dta, replace
			 clear
			 

			}
